package com.qianxunclub.context.config.swagger;

import com.google.common.collect.Sets;
import com.qianxunclub.context.service.MessageService;
import com.qianxunclub.util.SpringContextUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * Created by zhangbin on 2017/9/1.
 */
@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {

    @Bean
    public Docket MySwaggerConfig() {
        MessageService messageService = SpringContextUtil.getBean(MessageService.class);
        return new Docket(DocumentationType.SWAGGER_2)
                .produces(Sets.newHashSet("application/json", "application/xml"))
                .consumes(Sets.newHashSet("application/json", "application/xml"))
                .protocols(Sets.newHashSet("http", "https"))
                .host(messageService.message("swagger.apiHost"))
                .forCodeGeneration(true)
                .select().paths(regex(".*"))
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        MessageService messageService = SpringContextUtil.getBean(MessageService.class);
        ApiInfo apiInfo = new ApiInfo(
                messageService.message("swagger.title"),
                messageService.message("swagger.description"),
                messageService.message("swagger.version"),
                messageService.message("swagger.termsOfServiceUrl"),
                messageService.message("swagger.contact"),
                messageService.message("swagger.license"),
                messageService.message("swagger.licenseUrl")
        );
        return apiInfo;
    }
}
